package com.ssm.web.config.mq;

import com.ssm.common.constant.Constants;
import org.springframework.amqp.core.*;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AMQPConfig {

    @Bean
    public MessageConverter messageConverter() {
        return new Jackson2JsonMessageConverter();
    }

    /**
     * Topic 模式，交换机 Exchange
     */
    @Bean
    public Queue mqDataQueue() {
        return new Queue(Constants.MQ_DATA_QUEUE, true);
    }

    @Bean
    public TopicExchange topicExchage() {
        return new TopicExchange(Constants.TOPIC_EXCHANGE);
    }

    @Bean
    public Binding mqDataBinding() {
        return BindingBuilder.bind(mqDataQueue()).to(topicExchage()).with("topic." + Constants.MQ_DATA_QUEUE);
    }

    /**
     * 一个匿名的、不耐用的、独占的、自动删除的队列
     */
    @Bean
    public Queue webSocketNotifyQueue() {
        return new AnonymousQueue();
    }

    @Bean
    public FanoutExchange webSocketNotifyExchange() {
        return new FanoutExchange(Constants.WEBSOCKET_NOTIFY_EXCHANGE);
    }

    @Bean
    public Binding webSocketNotifyBinding() {
        return BindingBuilder.bind(webSocketNotifyQueue()).to(webSocketNotifyExchange());
    }
}